package com.lujianfei.netease_android_mvvm.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.lujianfei.netease_android_mvvm.R;
import com.lujianfei.netease_android_mvvm.databinding.AdapterMainFooterBinding;
import com.lujianfei.netease_android_mvvm.databinding.AdapterMainItemBinding;
import com.lujianfei.netease_android_mvvm.entity.DataBean;
import com.lujianfei.netease_android_mvvm.main.IMainMvvm;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

public class MainAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    final int TYPE_ITEM = 0;
    final int TYPE_FOOTER = 1;
    final int FOOTER_COUNT = 1;

    List<DataBean> mDataLists = new ArrayList<>();
    IMainMvvm.ViewModel mViewModel;
    boolean editMode;

    public MainAdapter(IMainMvvm.ViewModel mViewModel) {
        this.mViewModel = mViewModel;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case TYPE_ITEM: {
                ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.adapter_main_item, null, false);
                viewHolder = new MyViewHolder(binding);
            }
                break;
            case TYPE_FOOTER: {
                ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.adapter_main_footer, null, false);
                viewHolder = new MyFooterHolder(binding);
            }
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case TYPE_ITEM: {
                MyViewHolder viewHolder = (MyViewHolder) holder;
                AdapterMainItemBinding binding = ((AdapterMainItemBinding) viewHolder.getBinding());
                final DataBean dataList = mDataLists.get(position);
                binding.setItemData(dataList);
                binding.setEditMode(editMode);
                binding.setViewModel(this.mViewModel);
            }
                break;
            case TYPE_FOOTER: {
                MyFooterHolder viewHolder = (MyFooterHolder) holder;
                AdapterMainFooterBinding binding = ((AdapterMainFooterBinding) viewHolder.getBinding());
                binding.setEditMode(editMode);
            }
                break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (position == mDataLists.size()) {
            return TYPE_FOOTER;
        }
        return TYPE_ITEM;
    }

    @Override
    public int getItemCount() {
        return this.mDataLists.size() + FOOTER_COUNT;
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
        resetChecked();
        notifyItemRangeChanged(0,getItemCount());
    }

    public boolean isEditMode() {
        return editMode;
    }

    public void resetChecked() {
        for (DataBean dataList:
                mDataLists) {
            dataList.setChecked(false);
        }
    }

    public void setSelectAll(boolean selectAll) {
        for (DataBean dataList:
                mDataLists) {
            dataList.setChecked(selectAll);
        }
        notifyItemRangeChanged(0,getItemCount());
    }

    public void setData(List<DataBean> dataListList) {
        DiffUtil.calculateDiff(new MyDiffCallback(mDataLists,dataListList),true).dispatchUpdatesTo(this);
        mDataLists.clear();
        mDataLists.addAll(dataListList);
    }

    class MyDiffCallback extends DiffUtil.Callback {

        List<DataBean> oldDataListList;
        List<DataBean> newDataListList;

        public MyDiffCallback(List<DataBean> oldDataListList, List<DataBean> newDataListList) {
            this.oldDataListList = oldDataListList;
            this.newDataListList = newDataListList;
        }

        @Override
        public int getOldListSize() {
            return oldDataListList.size();
        }

        @Override
        public int getNewListSize() {
            return newDataListList.size();
        }

        @Override
        public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
            return oldDataListList.get(oldItemPosition).getTitle().equals(newDataListList.get(newItemPosition).getTitle());
        }

        @Override
        public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
            return oldDataListList.get(oldItemPosition).getTitle().equals(newDataListList.get(newItemPosition).getTitle());
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ViewDataBinding binding = null;

        public MyViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public ViewDataBinding getBinding() {
            return binding;
        }
    }
    class MyFooterHolder extends RecyclerView.ViewHolder {
        ViewDataBinding binding = null;

        public MyFooterHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public ViewDataBinding getBinding() {
            return binding;
        }
    }
}
